package com.hunan.api.catv.service.biz.tvv3;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.google.gson.JsonObject;
import com.hunan.api.catv.common.Constants;
import com.hunan.api.catv.common.ConstantsMsg;
import com.hunan.api.catv.entity.ServiceResult;
import com.hunan.api.catv.entity.dto.*;
import com.hunan.api.catv.entity.vo.TvReturnUserListVo;
import com.hunan.api.catv.mq.MqConstants;
import com.hunan.api.catv.service.ResultService;
import com.hunan.api.catv.service.biz.cache.CacheBiz;
import com.hunan.api.catv.service.biz.member.related.MemberAccountRelatedBiz;
import com.hunan.api.catv.service.biz.mq.MqBiz;
import com.hunan.api.catv.service.impl.tvv3.TvV3Service;
import com.hunan.api.catv.utils.GsonUtils;
import com.hunan.api.catv.utils.PageUtils;
import com.hunan.api.catv.utils.RedisUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jiang zhuan
 * @version 1.0
 * @description 描述
 * @date 2021/10/12 0012
 * @package com.hunan.api.catv.service.biz.tv
 * @company 湖南有线
 */
@Service
public class TvV3Biz extends ResultService {

    @Autowired
    CacheBiz cacheBiz;

    @Autowired
    TvV3Service tvV3Service;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    MqBiz mqBiz;

    @Autowired
    MemberAccountRelatedBiz accountRelatedBiz;

    private static final String TV_ACCESS_TOKEN = "TV::accessToken";
    private static final String TV_CHILD_TIME_LIMIT_CACHE = "TV::childTimeLimit";
    private static final String TV_START_MOD_CACHE = "TV::nowStartModCache";
    private static final String TV_MEMBER_ALL_START_MOD = "TV::memberStartMod.";
    private static final String TV_UNBIND_HN_USERNAME = "userNameNull";
    private static final String TV_V3_UN_LOGIN = "tvv3UnLogin";
    private static final String TV_GET_ACEESS_FAIL = "getAccessTokenFail";

    private String getAccesstoken(Integer memberId) {
        JsonObject conf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.tv_v3_service_conf), JsonObject.class);
        if (!ObjectUtils.isEmpty(conf)) {
            String domain = conf.get("domain_access").getAsString();
            Map<String, Object> paraMap = new HashMap<>();
            String userName = accountRelatedBiz.getAccountByMemberId(memberId);
            if (StringUtils.isBlank(userName)) {
                return TV_UNBIND_HN_USERNAME;
            }
            paraMap.put("userName", userName);
            paraMap.put("deviceType", conf.get("deviceType").getAsString());
            JsonObject info = tvV3Service.getAccesstoken(domain, paraMap);
            if (!ObjectUtils.isEmpty(info)) {
                log.info("{}", info);
                String status = info.get("ret").getAsString();
                if ("0".equals(status)) {
                    redisUtils.hset(TV_ACCESS_TOKEN, String.valueOf(memberId), info.get("access_token").getAsString());
                    return info.get("access_token").getAsString();
                }
                if ("9205".equals(status)) {
                    return TV_V3_UN_LOGIN;
                }
            }
        }
        return TV_GET_ACEESS_FAIL;
    }

    private ServiceResult setTerminalInfo(Integer memberId, TvQueryTerminalInfo terminalInfo) {
        JsonObject conf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.tv_v3_service_conf), JsonObject.class);
        if (!ObjectUtils.isEmpty(conf)) {
            String domain = conf.get("domain_access").getAsString();
            Object hget = redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId));
            if (ObjectUtils.isEmpty(hget)) {
                String accesstoken = getAccesstoken(memberId);
                if (TV_UNBIND_HN_USERNAME.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unbind_hn_username_fail);
                    return buildFail(ConstantsMsg.tv_unbind_hn_username_fail);
                }
                if (TV_V3_UN_LOGIN.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unlogin_fail);
                    return buildFail(ConstantsMsg.tv_unlogin_fail);
                }
                if (TV_GET_ACEESS_FAIL.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_getAccessToken_fail);
                    return buildFail(ConstantsMsg.tv_unifiedReturn_fail);
                }
            }
            terminalInfo.setAccesstoken((String) redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId)));
            JsonObject info = tvV3Service.setTerminalInfo(domain, terminalInfo);
            Map<Object, Object> hmget = redisUtils.hmget(TV_MEMBER_ALL_START_MOD + memberId);
            Map<Object, Object> reversionMap = hmget.entrySet().stream().collect(Collectors.toMap(o -> String.valueOf(o.getValue()), Map.Entry::getKey, (v1, v2) -> v1));
            if (!ObjectUtils.isEmpty(info)) {
                log.info("{}", info);
                String status = info.get("ret").getAsString();
                if ("0".equals(status)) {
                    Object before = redisUtils.hget(TV_START_MOD_CACHE, String.valueOf(memberId));
                    pushToMq(1, memberId, before, terminalInfo.getUserid(), ObjectUtils.isEmpty(reversionMap) ? "更改开机模式" : "开机模式从[" + reversionMap.get(String.valueOf(before)) + "]更改为[" + reversionMap.get(String.valueOf(terminalInfo.getUserid())) + "]");
                    redisUtils.hset(TV_START_MOD_CACHE, String.valueOf(memberId), terminalInfo.getUserid());
                    return buildOK(true);
                }
                if ("9021".equals(status)) {
                    String accesstoken = getAccesstoken(memberId);
                    if (TV_UNBIND_HN_USERNAME.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unbind_hn_username_fail);
                        return buildFail(ConstantsMsg.tv_unbind_hn_username_fail);
                    }
                    if (TV_V3_UN_LOGIN.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unlogin_fail);
                        return buildFail(ConstantsMsg.tv_unlogin_fail);
                    }
                    if (TV_GET_ACEESS_FAIL.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_getAccessToken_fail);
                        return buildFail(ConstantsMsg.tv_unifiedReturn_fail);
                    }
                    terminalInfo.setAccesstoken((String) redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId)));
                    info = tvV3Service.setTerminalInfo(domain, terminalInfo);
                    if (!ObjectUtils.isEmpty(info)) {
                        log.info("{}", info);
                        if ("0".equals(info.get("ret").getAsString())) {
                            Object before = redisUtils.hget(TV_START_MOD_CACHE, String.valueOf(memberId));
                            pushToMq(1, memberId, before, terminalInfo.getUserid(), ObjectUtils.isEmpty(reversionMap) ? "更改开机模式" : "开机模式从[" + reversionMap.get(String.valueOf(before)) + "]更改为[" + reversionMap.get(String.valueOf(terminalInfo.getUserid())) + "]");
                            redisUtils.hset(TV_START_MOD_CACHE, String.valueOf(memberId), terminalInfo.getUserid());
                            return buildOK(true);
                        }
                    }
                }
            }
        }
        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_setTerminalInfo_fail);
        return buildFail(ConstantsMsg.tv_unifiedReturn_fail);
    }

    private ServiceResult setTimeLimit(Integer memberId, TvQueryTimeLimitInfo timeLimitInfo) {
        JsonObject conf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.tv_v3_service_conf), JsonObject.class);
        if (!ObjectUtils.isEmpty(conf)) {
            String domain = conf.get("domain_access").getAsString();
            Object hget = redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId));
            if (ObjectUtils.isEmpty(hget)) {
                String accesstoken = getAccesstoken(memberId);
                if (TV_UNBIND_HN_USERNAME.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unbind_hn_username_fail);
                    return buildFail(ConstantsMsg.tv_unbind_hn_username_fail);
                }
                if (TV_V3_UN_LOGIN.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unlogin_fail);
                    return buildFail(ConstantsMsg.tv_unlogin_fail);
                }
                if (TV_GET_ACEESS_FAIL.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_getAccessToken_fail);
                    return buildFail(ConstantsMsg.tv_unifiedReturn_fail);
                }
            }
            timeLimitInfo.setAccesstoken((String) redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId)));
            JsonObject info = tvV3Service.setTimeLimit(domain, timeLimitInfo);
            if (!ObjectUtils.isEmpty(info)) {
                log.info("{}", info);
                String status = info.get("ret").getAsString();
                if ("0".equals(status)) {
                    Object before = redisUtils.hget(TV_CHILD_TIME_LIMIT_CACHE, String.valueOf(memberId));
                    redisUtils.hset(TV_CHILD_TIME_LIMIT_CACHE, String.valueOf(memberId), timeLimitInfo.getDuration());
                    pushToMq(2, memberId, before, String.valueOf(timeLimitInfo.getDuration()), "更改儿童模式限制时间");
                    return buildOK(true);
                }
                if ("9021".equals(status)) {
                    String accesstoken = getAccesstoken(memberId);
                    if (TV_UNBIND_HN_USERNAME.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unbind_hn_username_fail);
                        return buildFail(ConstantsMsg.tv_unbind_hn_username_fail);
                    }
                    if (TV_V3_UN_LOGIN.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unlogin_fail);
                        return buildFail(ConstantsMsg.tv_unlogin_fail);
                    }
                    if (TV_GET_ACEESS_FAIL.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_getAccessToken_fail);
                        return buildFail(ConstantsMsg.tv_unifiedReturn_fail);
                    }
                    timeLimitInfo.setAccesstoken((String) redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId)));
                    info = tvV3Service.setTimeLimit(domain, timeLimitInfo);
                    if (!ObjectUtils.isEmpty(info)) {
                        if ("0".equals(info.get("ret").getAsString())) {
                            log.info("{}", info);
                            Object before = redisUtils.hget(TV_CHILD_TIME_LIMIT_CACHE, String.valueOf(memberId));
                            redisUtils.hset(TV_CHILD_TIME_LIMIT_CACHE, String.valueOf(memberId), timeLimitInfo.getDuration());
                            pushToMq(2, memberId, before, String.valueOf(timeLimitInfo.getDuration()), "更改儿童模式限制时间");
                            return buildOK(true);
                        }
                    }
                }
            }
        }
        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_set_timelimit_fail);
        return buildFail(ConstantsMsg.tv_unifiedReturn_fail);
    }

    private void pushToMq(Integer type, Integer memberId, Object before, String after, String description) {
        JsonObject msg = new JsonObject();
        msg.addProperty("memberId", memberId);
        msg.addProperty("type", type);
        if (!ObjectUtils.isEmpty(before)) {
            msg.addProperty("before", String.valueOf(before));
        }
        if (!ObjectUtils.isEmpty(after)) {
            msg.addProperty("after", after);
        }
        if (!ObjectUtils.isEmpty(description)) {
            msg.addProperty("description", description);
        }
        mqBiz.push(MqConstants.TV_MODIFY_SET_EXCHANGE, MqConstants.TV_MODIFY_SET_ROUTINGKEY, GsonUtils.toJson(msg));
    }

    private JSONObject queryTimeLimit(Integer memberId, Integer userid) {
        JsonObject conf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.tv_v3_service_conf), JsonObject.class);
        if (!ObjectUtils.isEmpty(conf)) {
            String domain = conf.get("domain_access").getAsString();
            Map<String, Object> paraMap = new HashMap<>();
            Object hget = redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId));
            if (ObjectUtils.isEmpty(hget)) {
                String accesstoken = getAccesstoken(memberId);
                if (TV_UNBIND_HN_USERNAME.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unbind_hn_username_fail);
                    return new JSONObject().set("msg", ConstantsMsg.tv_unbind_hn_username_fail);
                }
                if (TV_V3_UN_LOGIN.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unlogin_fail);
                    return new JSONObject().set("msg", ConstantsMsg.tv_unlogin_fail);
                }
                if (TV_GET_ACEESS_FAIL.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_getAccessToken_fail);
                    return new JSONObject().set("msg", ConstantsMsg.tv_unifiedReturn_fail);
                }
            }
            paraMap.put("accesstoken", redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId)));
            if (userid != null) {
                paraMap.put("userid", userid);
            }
            JSONObject info = tvV3Service.queryTimeLimit(domain, paraMap);
            if (!ObjectUtils.isEmpty(info)) {
                log.info("{}", info);
                String status = info.getStr("ret");
                if ("0".equals(status)) {
                    return info;
                }
                if ("9021".equals(status)) {
                    String accesstoken = getAccesstoken(memberId);
                    if (TV_UNBIND_HN_USERNAME.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unbind_hn_username_fail);
                        return new JSONObject().set("msg", ConstantsMsg.tv_unbind_hn_username_fail);
                    }
                    if (TV_V3_UN_LOGIN.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unlogin_fail);
                        return new JSONObject().set("msg", ConstantsMsg.tv_unlogin_fail);
                    }
                    if (TV_GET_ACEESS_FAIL.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_getAccessToken_fail);
                        return new JSONObject().set("msg", ConstantsMsg.tv_unifiedReturn_fail);
                    }
                    paraMap.put("accesstoken", redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId)));
                    info = tvV3Service.queryTimeLimit(domain, paraMap);
                    if (!ObjectUtils.isEmpty(info)) {
                        log.info("{}", info);
                        if ("0".equals(info.getStr("ret"))) {
                            log.info("{}", info);
                            return info;
                        }
                    }
                }
            }
        }
        return null;
    }

    private JSONObject queryHistoryList(Integer memberId, Integer pageNum, Integer pageSize, Integer type) {
        JsonObject conf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.tv_v3_service_conf), JsonObject.class);
        if (!ObjectUtils.isEmpty(conf)) {
            String domain = conf.get("domain_slave").getAsString();
            Object hget = redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId));
            if (ObjectUtils.isEmpty(hget)) {
                String accesstoken = getAccesstoken(memberId);
                if (TV_UNBIND_HN_USERNAME.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unbind_hn_username_fail);
                    return new JSONObject().set("msg", ConstantsMsg.tv_unbind_hn_username_fail);
                }
                if (TV_V3_UN_LOGIN.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unlogin_fail);
                    return new JSONObject().set("msg", ConstantsMsg.tv_unlogin_fail);
                }
                if (TV_GET_ACEESS_FAIL.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_getAccessToken_fail);
                    return new JSONObject().set("msg", ConstantsMsg.tv_unifiedReturn_fail);
                }
            }
            TvQueryHistory tvQueryHistory = new TvQueryHistory();
            tvQueryHistory.setAccesstoken((String) redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId)));
            tvQueryHistory.setPageidx(pageNum);
            tvQueryHistory.setPagenum(pageSize);
            tvQueryHistory.setUserid("0");
            tvQueryHistory.setDays(7);
            if (type != 0) {
                tvQueryHistory.setType(String.valueOf(type));
            }
            Map<String, Object> paraMap = BeanUtil.beanToMap(tvQueryHistory);
            JSONObject info = tvV3Service.queryHistoryList(domain, paraMap);
            if (!ObjectUtils.isEmpty(info)) {
                log.info("{}", info);
                String status = info.getStr("ret");
                if ("0".equals(status) || "0.0".equals(status)) {
                    return info;
                }
                if ("9021".equals(status) || "9021.0".equals(status)) {
                    String accesstoken = getAccesstoken(memberId);
                    if (TV_UNBIND_HN_USERNAME.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unbind_hn_username_fail);
                        return new JSONObject().set("msg", ConstantsMsg.tv_unbind_hn_username_fail);
                    }
                    if (TV_V3_UN_LOGIN.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unlogin_fail);
                        return new JSONObject().set("msg", ConstantsMsg.tv_unlogin_fail);
                    }
                    if (TV_GET_ACEESS_FAIL.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_getAccessToken_fail);
                        return new JSONObject().set("msg", ConstantsMsg.tv_unifiedReturn_fail);
                    }
                    tvQueryHistory.setAccesstoken((String) redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId)));
                    paraMap = BeanUtil.beanToMap(tvQueryHistory);
                    info = tvV3Service.queryHistoryList(domain, paraMap);
                    if (!ObjectUtils.isEmpty(info)) {
                        if ("0".equals(info.getStr("ret"))) {
                            log.info("{}", info);
                            return info;
                        }
                    }
                }
            }
        }
        return null;
    }

    private JSONObject queryUserList(Integer memberId) {
        JsonObject conf = GsonUtils.fromJson(cacheBiz.getSystemDict(Constants.tv_v3_service_conf), JsonObject.class);
        if (!ObjectUtils.isEmpty(conf)) {
            String domain = conf.get("domain_access").getAsString();
            Map<String, Object> paraMap = new HashMap<>();
            paraMap.put("pageidx", 1);
            paraMap.put("pagenum", 100);
            Object hget = redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId));
            if (ObjectUtils.isEmpty(hget)) {
                String accesstoken = getAccesstoken(memberId);
                if (TV_UNBIND_HN_USERNAME.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unbind_hn_username_fail);
                    return new JSONObject().set("msg", ConstantsMsg.tv_unbind_hn_username_fail);
                }
                if (TV_V3_UN_LOGIN.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unlogin_fail);
                    return new JSONObject().set("msg", ConstantsMsg.tv_unlogin_fail);
                }
                if (TV_GET_ACEESS_FAIL.equals(accesstoken)) {
                    log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_getAccessToken_fail);
                    return new JSONObject().set("msg", ConstantsMsg.tv_unifiedReturn_fail);
                }
            }
            paraMap.put("accesstoken", redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId)));
            JSONObject info = tvV3Service.queryUserList(domain, paraMap);
            if (!ObjectUtils.isEmpty(info)) {
                log.info("{}", info);
                String status = info.getStr("ret");
                if ("0".equals(status)) {
                    return info;
                }
                if ("9021".equals(status)) {
                    String accesstoken = getAccesstoken(memberId);
                    if (TV_UNBIND_HN_USERNAME.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unbind_hn_username_fail);
                        return new JSONObject().set("msg", ConstantsMsg.tv_unbind_hn_username_fail);
                    }
                    if (TV_V3_UN_LOGIN.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_unlogin_fail);
                        return new JSONObject().set("msg", ConstantsMsg.tv_unlogin_fail);
                    }
                    if (TV_GET_ACEESS_FAIL.equals(accesstoken)) {
                        log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_getAccessToken_fail);
                        return new JSONObject().set("msg", ConstantsMsg.tv_unifiedReturn_fail);
                    }
                    paraMap.put("accesstoken", redisUtils.hget(TV_ACCESS_TOKEN, String.valueOf(memberId)));
                    info = tvV3Service.queryUserList(domain, paraMap);
                    if (!ObjectUtils.isEmpty(info)) {
                        log.info("{}", info);
                        if ("0".equals(info.getStr("ret"))) {
                            log.info("{}", info);
                            return info;
                        }
                    }
                }
            }
        }
        return null;
    }

    public ServiceResult chageStartMode(Integer memberId, String id) {
        TvQueryTerminalInfo terminalInfo = new TvQueryTerminalInfo();
        terminalInfo.setUserid(id);
        terminalInfo.setIsdefault(1);
        return setTerminalInfo(memberId, terminalInfo);
    }

    public Object queryStartMode(Integer memberId) {
        JSONObject o = queryUserList(memberId);
        if (ObjectUtils.isEmpty(o)) {
            log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_queryUserList_fail);
            return buildFail(ConstantsMsg.tv_unifiedReturn_fail);
        }
        if (StringUtils.isNotBlank(o.getStr("msg"))) {
            return buildFail(o.getStr("msg"));
        }
        JSONObject jsonObject = JSONUtil.parseObj(o);
        TvReturnUserList tvReturnUserList = jsonObject.toBean(TvReturnUserList.class);
        List<TvReturnUserList.UserList> userList = tvReturnUserList.getUser_list();
        Map<String, String> map = userList.parallelStream().collect(Collectors.toMap(o1 -> String.valueOf(o1.getUser_id()), TvReturnUserList.UserList::getNick_name, (v1, v2) -> v1));
        Map<Object, Object> hmget = redisUtils.hmget(TV_MEMBER_ALL_START_MOD + memberId);
        if (ObjectUtils.isEmpty(hmget)) {
            Map<Object, Object> saveMap = userList.parallelStream().collect(Collectors.toMap(TvReturnUserList.UserList::getNick_name, TvReturnUserList.UserList::getUser_id, (v1, v2) -> v1));
            redisUtils.hmset(TV_MEMBER_ALL_START_MOD + memberId, saveMap);
        }
        TvReturnUserListVo tvReturnUserListVo = new TvReturnUserListVo();
        BeanUtil.copyProperties(tvReturnUserList, tvReturnUserListVo);
        tvReturnUserListVo.setBind_user_id(tvReturnUserList.getDefault_user_id());
        tvReturnUserListVo.setBind_user_name(map.get(String.valueOf(tvReturnUserList.getDefault_user_id())));
        List<TvReturnUserListVo.UserListVo> user_list = tvReturnUserListVo.getUser_list();
        user_list.forEach(userListVo -> userListVo.setUser_name(map.get(String.valueOf(userListVo.getUser_id()))));
        return tvReturnUserListVo;
    }

    public ServiceResult changeChildModeTimeLimit(Integer memberId, Integer minute) {
        TvQueryTimeLimitInfo tvQueryTimeLimitInfo = new TvQueryTimeLimitInfo();
        if (minute == null || minute == 0) {
            tvQueryTimeLimitInfo.setSwitchFlag(0);
        } else {
            tvQueryTimeLimitInfo.setDuration(minute);
            tvQueryTimeLimitInfo.setSwitchFlag(1);
        }
        Map<Object, Object> hmget = redisUtils.hmget(TV_MEMBER_ALL_START_MOD + memberId);
        if (ObjectUtils.isEmpty(hmget)) {
            queryStartMode(memberId);
        }
        tvQueryTimeLimitInfo.setUserid((Integer) hmget.get("儿童"));
        return setTimeLimit(memberId, tvQueryTimeLimitInfo);
    }

    public Object queryChildModeTimeLimit(Integer memberId) {
        JsonObject result = new JsonObject();
        Map<Object, Object> hmget = redisUtils.hmget(TV_MEMBER_ALL_START_MOD + memberId);
        if (ObjectUtils.isEmpty(hmget)) {
            queryStartMode(memberId);
        }
        JSONObject o = queryTimeLimit(memberId, (Integer) hmget.get("儿童"));
        if (ObjectUtils.isEmpty(o)) {
            log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_queryTimeLimit_fail);
            return buildFail(ConstantsMsg.tv_unifiedReturn_fail);
        }
        if (StringUtils.isNotBlank(o.getStr("msg"))) {
            return buildFail(o.getStr("msg"));
        }
        JSONObject jsonObject = JSONUtil.parseObj(o);
        TvReturnTimeLimitInfo limitInfo = jsonObject.toBean(TvReturnTimeLimitInfo.class);
        result.addProperty("minute", limitInfo.getWork_time().getDuration());
        return result;
    }

    public Object queryTvHistory(Integer memberId, Integer pageNum, Integer pageSize, Integer type) {
        JSONObject o = queryHistoryList(memberId, pageNum, pageSize, type);
        if (ObjectUtils.isEmpty(o)) {
            log.info("调用TV工具接口失败：{}-{}", memberId, ConstantsMsg.tv_queryTvHistory_fail);
            return buildFail(ConstantsMsg.tv_unifiedReturn_fail);
        }
        if (StringUtils.isNotBlank(o.getStr("msg"))) {
            return buildFail(o.getStr("msg"));
        }
        JSONObject jsonObject = JSONUtil.parseObj(o);
        JSONArray list = jsonObject.getJSONArray("list");
        Map<String, String> map = new HashMap<>();
        Iterator<JSONObject> it = list.jsonIter().iterator();
        while (it.hasNext()) {
            JSONObject next = it.next();
            map.put(String.valueOf(next.getInt("id")), next.getStr("abstract"));
        }

        TvReturnHistory tvReturnHistory = jsonObject.toBean(TvReturnHistory.class);
        tvReturnHistory.getList().forEach(tvWatchItem -> {
            tvWatchItem.setContent(map.get(String.valueOf(tvWatchItem.getId())));
            if (StringUtils.isNotBlank(tvWatchItem.getPoster())) {
                tvWatchItem.setPoster(JSONUtil.parseObj(tvWatchItem.getPoster()).getStr("default"));
            }
            if (StringUtils.isNotBlank(tvWatchItem.getLast_used_time())) {
                tvWatchItem.setLast_used_time(LocalDateTime.ofEpochSecond(Double.valueOf(tvWatchItem.getLast_used_time()).intValue(), 0, ZoneOffset.of("+8")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
            }
            tvWatchItem.setProgress(tvWatchItem.getDuration() == 0 ? 0 : Double.valueOf(tvWatchItem.getOfftime()) / Double.valueOf(tvWatchItem.getDuration()) > 0.995 ? 100 : Double.valueOf(tvWatchItem.getOfftime()) / Double.valueOf(tvWatchItem.getDuration()) * 100);
        });

        return PageUtils.list2PageInfoNotAllData(pageNum, pageSize, tvReturnHistory.getList(), tvReturnHistory.getTotal());
    }

}
